How does Python handle arguments in functions (pass-by-value or pass-by-reference)?
How does Python handle arguments in functions (pass-by-value or pass-by-reference)?
Ravi Vishwakarma is a dedicated Software Developer with a passion for crafting efficient and innovative solutions. With a keen eye for detail and years of experience, he excels in developing robust software systems that meet client needs. His expertise spans across multiple programming languages and technologies, making him a valuable asset in any software development project.
Khushi Singh
15-Apr-2025The memory transmission mechanism in Python combines features from both pass-by-value and pass-by-reference into what is best described as “pass-by-object-reference” or “pass-by-assignment”. Functions accept argument references only instead of receiving the original objects when they are invoked. The ability to modify objects by functions depends on the object type since mutable elements allow modifications while immutable elements do not.
A mutable object passed to a function allows changes made within the function to modify both the original object located outside the function boundaries. Assigning a new object through the function parameter has no effect on the initial object stored outside the function.
Modification attempts on immutable objects within a function do not impact the original object outside the function because the function instead binds a new object reference to its local scope.
Understandably this operational style causes ambiguity when developers expect Java-like value passing or C++-like reference copying behaviors. Inside Python functions users can receive object references but the function prevents modifications of these references by external code.
Code Example
The example shows the list becomes modified since it is mutable yet the integer stays unchanged because it is immutable.